#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
#include <stdio.h>

using namespace std;
using PII = pair<int, int>;

const int N = 25010, M = 150010, INF = 0x3f3f3f3f;

int n, mr, mp, S;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool state[N];
int id[N];      //每个点属于哪个连通块
int bcnt = 0;
int deg[N];
vector<int> block[N];
queue<int> q;       //topologo算法


void add(int a, int b, int c){
    e[idx] = b;
    ne[idx] = h[a];
    w[idx] = c;
    h[a] = idx++;
}

void dfs(int i, int bcnt){
    id[i] = bcnt;
    block[bcnt].push_back(i);

    for (int u = h[i]; u != -1; u = ne[u]){
        int j = e[u];
        if(!id[j]){
            dfs(j, bcnt);
        }
    }
}

void dijkstra(int k){
    priority_queue<PII, vector<PII>, greater<PII>> que;

    for(auto temp : block[k]){
        que.push({dist[temp], temp});
    }

    while(que.size()){
        PII temp = que.top();
        que.pop();

        int ver = temp.second, dis = temp.first;
        if(state[ver]) continue;

        state[ver] = true;

        for (int u = h[ver]; u != -1; u = ne[u]){
            int j = e[u];
            if(id[j] != id[ver] && --deg[id[j]] == 0){
                q.push(id[j]);
            }
            if(dist[j] > dis + w[u]){
                dist[j] = dis + w[u];
                if(id[j] == id[ver]){
                    que.push({dist[j], j});
                }
            }
        }
    }
}

void topologo(){
    for (int i = 1; i <= bcnt; i ++){
        if(deg[i] == 0){
            q.push(i);
        }
    }

    while(q.size()){
        int k = q.front();
        q.pop();

        dijkstra(k);
    }
}

int main()
{
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);

    memset(h, -1, sizeof h);
    memset(dist, 0x3f, sizeof dist);
    scanf("%d%d%d%d", &n, &mr, &mp, &S);

    dist[S] = 0;
    for (int i = 0; i < mr; i ++){
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
        add(b, a, c);
    }


    //创建连通块
    for (int i = 1; i <= n; i ++){
        if(!id[i]){
            bcnt++;
            dfs(i, bcnt);
        }
    }

    //计算入度
    for (int i = 0; i < mp; i++)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
        deg[id[b]]++;
    }

    topologo();

    for (int i = 1; i <= n; i++)
    {
        if (dist[i] > INF / 2){
            puts("NO PATH");
        }else{
            printf("%d\n", dist[i]);
        }
    }
    return 0;
}